package com.bw.house.controller;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.bw.house.entity.HouseNewroomEntity;
import com.bw.house.entity.HouseRentEntity;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bw.house.service.HouseRentService;
import com.bw.common.utils.PageUtils;
import com.bw.common.utils.R;



/**
 * 租房信息表
 *
 * @author Hao_YunKai
 * @email 2745578447@qq.com
 * @date 2022-03-23 11:46:46
 */
@RestController
@RequestMapping("house/house-rent")
public class HouseRentController {
    @Autowired
    private HouseRentService houseRentService;


    @SuppressWarnings("all")
    @Autowired
    private RestHighLevelClient client;

    /**
     * 列表
     */
    @RequestMapping("/ZuFangEsList")
    public R zuFangEsList(@RequestParam Map<String, Object> params) throws IOException {
        PageUtils page = houseRentService.zuFangEsList(params);
        return R.ok().put("page", page);
    }
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) throws IOException {
        PageUtils page = houseRentService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{rentId}")
    public R info(@PathVariable("rentId") Long rentId){
		HouseRentEntity houseRent = houseRentService.getById(rentId);

        return R.ok().put("houseRent", houseRent);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody HouseRentEntity houseRent){
		houseRentService.save(houseRent);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody HouseRentEntity houseRent){
		houseRentService.updateById(houseRent);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] rentIds){
		houseRentService.removeByIds(Arrays.asList(rentIds));

        return R.ok();
    }


    @RequestMapping("ZuFangEs")
    public R ZuFangEs() throws IOException {
        List<HouseRentEntity> list = houseRentService.list(null);
        BulkRequest request = new BulkRequest();
        for (HouseRentEntity HouseRentEntity : list) {
            IndexRequest request1 = new IndexRequest("zufang");
            request1.id(HouseRentEntity.getRentId()+"");
            String s = JSON.toJSONString(HouseRentEntity);
            request1.source(s, XContentType.JSON);
            request.add(request1);
        }
        BulkResponse bulkResponse = client.bulk(request, RequestOptions.DEFAULT);
        return R.ok().put("result","租房es成功");
    }


}
